package com.xiaoyg.algorithm;

import java.util.Arrays;

/***
 * 数组中项
 */
public class ArrayCenterIndex {


    public static void main(String[] args) {
        System.out.println(pivotIndex(new int []{1,7,3,6,5,6,4}));
    }
    //一个total从左至右累加，一个sum总值递减，直到二值相等返回index
    private static int pivotIndex(int[] nums) {
        int sum = Arrays.stream(nums).sum();

        int total = 0;
        for (int i = 0; i < nums.length; i++) {
            total+= nums[i];
            if (total == sum){
                return i;
            }
            sum = sum - nums[i];
        }
        return -1;
    }

}
